package movie.controller;

import java.text.SimpleDateFormat;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import movie.entity.Cinema;
import movie.entity.CinemaName;
import movie.entity.Film;
import movie.entity.Hall;
import movie.entity.OrderDetail;
import movie.service.CinemaNameService;
import movie.service.CinemaScheduleFindAllService;
import movie.service.FilmFindAllService;
import movie.service.HallService;
import movie.service.OrderDetailService;


@Controller
public class CinemaFindAllController {

	@Autowired
	private FilmFindAllService filmFindAllService;
	@Autowired
	private CinemaScheduleFindAllService cinemaScheduleFindAllService;
	@Autowired
	private CinemaNameService cinemaNameService;
	@Autowired
	private HallService hallService;
	@Autowired
	private OrderDetailService orderDetailService;
	
	
	@RequestMapping(value = "/cinemaFindAll", method = RequestMethod.GET)
	public String get(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		
		List<Film> films = filmFindAllService.findAll();

		for(Film film:films)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			film.setReleaseDateString(format1.format(film.getReleaseDate()));			
		}
		
		model.addAttribute("films", films);
		
		
		int cinemaId=(int) session.getAttribute("cinemaId");//
		
		model.addAttribute("OrderNumberByCinemaId",orderDetailService.findOrderNumberByCinemaId(cinemaId));
		List<OrderDetail> orderDetails=orderDetailService.findPriceByCinemaId(cinemaId);
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("priceByCinemaId",orderDetails);
		
		return "Cinema/filmlist";
	}
	
	@RequestMapping(value = "/cinemaFindAllFilm", method = RequestMethod.GET)
	public String filmGet(Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		int movieId = (int) session.getAttribute("movieId");
		
		int cinemaId=(int) session.getAttribute("cinemaId");    
		
		
		
		Cinema cinema = new Cinema();
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		
		for(Cinema cinema11:cinemas)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
			cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
		}
		
		model.addAttribute("cinemas", cinemas);
		
		return "Cinema/cinemalist";
	}
	
	@RequestMapping(value = "/cinemaFindAllFilm", method = RequestMethod.POST)
	public String filmpPost(int movieId,Model model,HttpServletRequest request) {
		model.addAttribute("movieId", movieId);
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		
		int cinemaId=(int) session.getAttribute("cinemaId");        //
		//session.setAttribute("cinemaId", cinemaId);    //
		
		Cinema cinema = new Cinema();
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		List<Cinema> cinemas=cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
		
		for(Cinema cinema11:cinemas)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
			cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
		}
		
		model.addAttribute("cinemas", cinemas);
		
		session.setAttribute("movieId", movieId);
		
		
		return "Cinema/cinemalist";
	}
	
	@RequestMapping(value = "/cinemaupdate", method = RequestMethod.GET)
	public String getUpdate(Model model,int scheduleId,HttpServletRequest request) {
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		Cinema cinema=cinemaScheduleFindAllService.findByScheduleId(scheduleId);


		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
		cinema.setFilmBeginTimeString(format1.format(cinema.getFilmBeginTime()));
		cinema.setFilmEndTimeString(format1.format(cinema.getFilmEndTime()));			
		
		model.addAttribute("cinema", cinema);
		
		
		List<String> cinemaNames=hallService.findByCinemaIdToHallName(cinema.getCinemaId());
		model.addAttribute("cinemaNames", cinemaNames);
		
		return "Cinema/cinemaupdate";
	}
	
	@RequestMapping(value = "/cinemaupdate", method = RequestMethod.POST)
	public String postUpdate(Model model,Cinema cinema,String cinemaName1,String hallName1,HttpServletRequest request) {
		
		if(cinema.getFilmBeginTime()==null || cinema.getBeginHour()==null || cinema.getBeginMin()==null || cinema.getMovieId()==null
				|| cinemaName1.equals("") || hallName1.equals("") || cinema.getFilmEndTime()==null || cinema.getFilmLanguage().equals("") 
				|| cinema.getPrice()==null || cinema.getEndHour()==null || cinema.getEndMin()==null ||cinema.getFilmVersion().equals("") )
		{
			HttpSession session = request.getSession();

			if(session.getAttribute("cinemaId") == null) {
				return "forward:clogin.jsp";
			} 
			Cinema cinema22=cinemaScheduleFindAllService.findByScheduleId(cinema.getScheduleId());

			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			cinema22.setFilmBeginTimeString(format1.format(cinema22.getFilmBeginTime()));
			cinema22.setFilmEndTimeString(format1.format(cinema22.getFilmEndTime()));			
			
			model.addAttribute("cinema", cinema22);
			
			
			List<String> cinemaNames=hallService.findByCinemaIdToHallName(cinema22.getCinemaId());
			model.addAttribute("cinemaNames", cinemaNames);
			model.addAttribute("cuowu", 1);
			return "Cinema/cinemaupdate";
		}
		
		
		
		Hall hall=new Hall();
		int cinemaId=cinemaNameService.findByCinemaNameToCinemaId(cinemaName1);
		cinema.setCinemaId(cinemaId);
		hall.setCinemaId(cinemaId);
		hall.setHallName(hallName1);
		int hallId=hallService.findByCinemaIdHallNameToHallId(hall);
		cinema.setHallId(hallId);
		int row=cinemaScheduleFindAllService.update(cinema);
		model.addAttribute("cinema", cinema);
		
		return "redirect:/cinemaFindAllFilm";
	}
	
	@RequestMapping(value = "/filmfind/filmname", method = RequestMethod.GET)
	public String postfilmname(String filmName,Model model,HttpServletRequest request) {
		HttpSession session = request.getSession();

		if(session.getAttribute("cinemaId") == null) {
			return "forward:clogin.jsp";
		} 
		
		int cinemaId=(int) session.getAttribute("cinemaId");   
		model.addAttribute("OrderNumberByCinemaId",orderDetailService.findOrderNumberByCinemaId(cinemaId));
		List<OrderDetail> orderDetails=orderDetailService.findPriceByCinemaId(cinemaId);
		for(OrderDetail orderDetail11:orderDetails)
		{
			SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
			orderDetail11.setOrderDateString(format1.format(orderDetail11.getOrderDate()));			
		}
		model.addAttribute("priceByCinemaId",orderDetails);
		if(!filmName.equals(""))
		{
			List<Film> films = filmFindAllService.findByFilmName(filmName);
			for(Film film:films)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));			
			}
			model.addAttribute("films", films);
			return "Cinema/filmlist";
		}
		else 
		{
			List<Film> films = filmFindAllService.findAll();
			for(Film film:films)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				film.setReleaseDateString(format1.format(film.getReleaseDate()));			
			}
			model.addAttribute("films", films);
			return "Cinema/filmlist";
		}
				
	}

	/**
	 * 进入到电影院列表
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/cinemas", method = RequestMethod.GET)
	public String getAllCinema(Model model) {
		List<CinemaName> cinemas=cinemaNameService.findAll();
		model.addAttribute("cinemas", cinemas);
		return "/Cinema/cinemas";			
	}
	
	
	@RequestMapping(value = "/cinemas/{cid}", method = RequestMethod.GET)
	public String getCinemaFilms(@PathVariable("cid") int cinemaId,Model model) {
		List<Film> films=cinemaScheduleFindAllService.findAllFilm(cinemaId);
		model.addAttribute("cinemaId", cinemaId);
		CinemaName cinema=cinemaNameService.findCinema(cinemaId);
		//System.out.println(films);
		model.addAttribute("films", films);
		model.addAttribute("cinema", cinema);
		return "/Cinema/CinemaShow";			
	}

	@RequestMapping(value = "/cinemafind/hallname", method = RequestMethod.GET)
	public String findHallGet(String hallName,Model model,HttpServletRequest request)
	{
		HttpSession session = request.getSession();
		int movieId = (int) session.getAttribute("movieId");
		int cinemaId=(int) session.getAttribute("cinemaId");
		Hall hall=new Hall();
		hall.setHallName(hallName);
		Cinema cinema=new Cinema();
		cinema.setHall(hall);
		cinema.setMovieId(movieId);
		cinema.setCinemaId(cinemaId);
		if(!hallName.equals(""))
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaIdHallName(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemalist";
		}
		else 
		{
			List<Cinema> cinemas = cinemaScheduleFindAllService.findByMovieIdCinemaId(cinema);
			for(Cinema cinema11:cinemas)
			{
				SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");		
				cinema11.setFilmBeginTimeString(format1.format(cinema11.getFilmBeginTime()));
				cinema11.setFilmEndTimeString(format1.format(cinema11.getFilmEndTime()));			
			}
			model.addAttribute("cinemas", cinemas);
			return "Cinema/cinemalist";
		}
	}
	
}
